
@inject LogCacheService LogCacheService

<div class="log-viewer">
    <div class="log-header">
        <h4>系统日志</h4>
        <div class="log-controls">
            <button class="btn btn-sm btn-outline-secondary" @onclick="ClearLogs">清空日志</button>
            <span class="badge badge-info">@LogCacheService.Count 条日志</span>
        </div>
    </div>
    
    <div class="log-container">
        @if (logs.Any())
        {
            <div class="log-list">
                @foreach (var log in logs.OrderByDescending(log => log.Timestamp))
                {
                    <div class="log-item @GetLogLevelClass(log.Level)">
                        <div class="log-time">@log.Timestamp.ToString("HH:mm:ss.fff")</div>
                        <div class="log-level">@log.Level</div>
                        <div class="log-message">@log.Message</div>
                        @if (log.Exception != null)
                        {
                            <div class="log-exception">@log.Exception.Message</div>
                        }
                    </div>
                }
            </div>
        }
        else
        {
            <div class="text-muted text-center p-3">暂无日志</div>
        }
    </div>
</div>

<style>
    .log-viewer {
        height: 400px;
        border: 1px solid #dee2e6;
        border-radius: 0.25rem;
        display: flex;
        flex-direction: column;
    }
    
    .log-header {
        padding: 0.75rem;
        border-bottom: 1px solid #dee2e6;
        display: flex;
        justify-content: space-between;
        align-items: center;
        background-color: #f8f9fa;
    }
    
    .log-controls {
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }
    
    .log-container {
        flex: 1;
        overflow-y: auto;
    }
    
    .log-list {
        padding: 0;
        margin: 0;
        list-style: none;
    }
    
    .log-item {
        padding: 0.5rem 0.75rem;
        border-bottom: 1px solid #f0f0f0;
        display: flex;
        align-items: center;
        gap: 0.75rem;
        font-family: 'Courier New', monospace;
        font-size: 0.875rem;
    }
    
    .log-item:hover {
        background-color: #f8f9fa;
    }
    
    .log-time {
        color: #6c757d;
        min-width: 80px;
        font-size: 0.75rem;
    }
    
    .log-level {
        min-width: 80px;
        font-weight: bold;
        text-align: center;
        padding: 0.125rem 0.25rem;
        border-radius: 0.125rem;
        font-size: 0.75rem;
    }
    
    .log-message {
        flex: 1;
        word-break: break-all;
    }
    
    .log-exception {
        color: #dc3545;
        font-style: italic;
    }
    
    .log-trace .log-level { background-color: #e9ecef; color: #6c757d; }
    .log-debug .log-level { background-color: #d1ecf1; color: #0c5460; }
    .log-information .log-level { background-color: #d4edda; color: #155724; }
    .log-warning .log-level { background-color: #fff3cd; color: #856404; }
    .log-error .log-level { background-color: #f8d7da; color: #721c24; }
    .log-critical .log-level { background-color: #f5c6cb; color: #721c24; }
</style>

@code {
    private List<LogEntry> logs = new List<LogEntry>();
    
    protected override void OnInitialized()
    {
        // 加载现有日志并倒序显示（最新的在最上面）
        logs = LogCacheService.GetLogs().OrderByDescending(log => log.Timestamp).ToList();
        
        // 订阅日志添加事件
        LogCacheService.OnLogAdded += OnLogAdded;
    }
    
    private void OnLogAdded(LogEntry logEntry)
    {
        // 添加新日志到列表并保持倒序排列
        logs.Add(logEntry);
        logs = logs.OrderByDescending(log => log.Timestamp).ToList();
        
        // 如果日志数量超过1000条，移除最旧的
        if (logs.Count > 1000)
        {
            logs = logs.Take(1000).ToList();
        }
        
        // 刷新UI
        InvokeAsync(StateHasChanged);
    }
    
    private void ClearLogs()
    {
        LogWrapper.Clear();
        logs.Clear();
        StateHasChanged();
    }
    
    private string GetLogLevelClass(LogLevel level)
    {
        return level switch
        {
            LogLevel.Trace => "log-trace",
            LogLevel.Debug => "log-debug",
            LogLevel.Information => "log-information",
            LogLevel.Warning => "log-warning",
            LogLevel.Error => "log-error",
            LogLevel.Critical => "log-critical",
            _ => "log-information"
        };
    }
    
    public void Dispose()
    {
        LogCacheService.OnLogAdded -= OnLogAdded;
    }
}